
********************************************************************************
****************************************GRAPHS**********************************
********************************************************************************

 
* Figure3 -  Even t-DD // Diff_in_Diff By Country 
*ssc install eventdd
*ssc install matsort
* * * * *
clear all
use "$dataFinalmaster.dta", clear

****************************************
ppmlhdfe aggregate_students_origin treatment_var if sample_==1 & year>=2003 & year<=2017, absorb(it jt ji) cluster(ji)
gen sample_ppml = e(sample)


* CHINA - Non-Treated Nationalities * 
preserve
drop if Origin=="India" | Origin=="China" | Origin=="Vietnam"
gen year_=2010
foreach v of var year_{
	replace `v'=. if dumvarChina_treatlist==. 
}
move year_ year
move dumvarChina_treatlist  year_
drop Province_of_Study ijt Province Origin
move treatment_var aggregate_students_origin 
* 2011
replace year_=2011 if Name_Institution=="Canadore College of Applied Arts and Technology" | Name_Institution=="Langara College" | Name_Institution=="New Brunswick Community College"
* 2012
replace year_=2012 if Name_Institution=="Cape Breton University" | Name_Institution=="College of the North Atlantic - Parent Institution" | Name_Institution=="Saskatchewan Polytechnic - Parent Institution"
* Setting time to treat
gen timeToTreat= (year - year_)
tab timeToTreat
* Ihs transformation 
gen ihs_aggregate_students_origin = log(aggregate_students_origin + sqrt(aggregate_students_origin^2+1) )
* log_transformation
gen log_aggregate_students_origin = log(1+aggregate_students_origin)
gen log_aggregate_students_origin_ = log(aggregate_students_origin)
bysort Name_Institution year: egen aggregate_nt_studs = total(aggregate_students_origin)
duplicates drop Name_Institution year, force
* Event DD PPML
replace dumvarChina_treatlist=0 if dumvarChina_treatlist==.
* Alternative SE clustering
quietly ppmlhdfe aggregate_nt_studs ib2009.year##i.dumvarChina_treatlist if sample_==1 & year>=2004 & year<=2017, absorb(Insitution_Code_5Digit year) cluster(Insitution_Code_5Digit)
local plotopts ytitle("Estimate and 95% Conf. Int.") title("China")
coefplot, vertical yline(0) ciopts(recast(rcap)) xlabel(,angle(45)) `plotopts' drop(_cons) scheme(s1mono) xline(5.5)
restore

* CHINA*  Treated Nationalities *  
preserve
keep if Origin=="China"
gen year_=2010
foreach v of var year_{
	replace `v'=. if dumvarChina_treatlist==. 
}
move year_ year
move dumvarChina_treatlist  year_
drop Province_of_Study ijt Province Origin
move treatment_var aggregate_students_origin 
* 2011
replace year_=2011 if Name_Institution=="Canadore College of Applied Arts and Technology" | Name_Institution=="Langara College" | Name_Institution=="New Brunswick Community College"
* 2012
replace year_=2012 if Name_Institution=="Cape Breton University" | Name_Institution=="College of the North Atlantic - Parent Institution" | Name_Institution=="Saskatchewan Polytechnic - Parent Institution"
* Setting time to treat
gen timeToTreat= (year - year_)
tab timeToTreat
* Ihs transformation 
gen ihs_aggregate_students_origin = log(aggregate_students_origin + sqrt(aggregate_students_origin^2+1) )
* log_transformation
gen log_aggregate_students_origin = log(1+aggregate_students_origin)
gen log_aggregate_students_origin_ = log(aggregate_students_origin)
* Event DD PPML
replace dumvarChina_treatlist=0 if dumvarChina_treatlist==.
quietly ppmlhdfe aggregate_students_origin ib2009.year##i.dumvarChina_treatlist if sample_==1 & year>=2004 & year<=2017, absorb(Insitution_Code_5Digit year) cluster(Insitution_Code_5Digit)
local plotopts ytitle("Estimate and 95% Conf. Int.") title("China")
coefplot, vertical yline(0) ciopts(recast(rcap)) xlabel(,angle(45)) `plotopts' drop(_cons) scheme(s1mono) xline(5.5)
restore



* INDIA - Non-Treated Nationalities * 
preserve
drop if Origin=="India" | Origin=="China" | Origin=="Vietnam"
gen year_=2009
foreach v of var year_{
	replace `v'=. if dumvarIndia_treatlist==. 
}
move year_ year
move dumvarIndia_treatlist  year_
drop Province_of_Study ijt Province Origin
move treatment_var aggregate_students_origin 
* 2010
replace year_=2010 if Name_Institution=="College of New Caledonia" | Name_Institution=="College of the Rockies" | Name_Institution=="Conestoga College of Applied Arts and Technology" | Name_Institution=="Confederation College of Applied Arts and Technology" | Name_Institution=="Douglas College" | Name_Institution=="Durham College of Applied Arts and Technology" | Name_Institution=="Fanshawe College of Applied Arts and Technology" | Name_Institution=="Kwantlen Polytechnic University" | Name_Institution=="Lambton College of Applied Arts and Technology" | Name_Institution=="Medicine Hat College" | Name_Institution=="North Island College" | Name_Institution=="Northern Lights College" | Name_Institution=="Okanagan College" | Name_Institution=="Selkirk College" | Name_Institution=="St. Clair College of Applied Arts and Technology" | Name_Institution=="St. Lawrence College of Applied Arts and Technology - Parent Institution" 
* 2011
replace year_=2011 if Name_Institution=="New Brunswick Community College" | Name_Institution=="Saskatchewan Polytechnic - Parent Institution"
* 2012
replace year_=2012 if Name_Institution=="Langara College"
* 2015
replace year_=2015 if Name_Institution=="Assiniboine Community College" | Name_Institution=="Canadore College of Applied Arts and Technology" | Name_Institution=="Collège LaSalle" | Name_Institution=="Lakeland College" | Name_Institution=="NorQuest College" | Name_Institution=="Sault College of Applied Arts and Technology - Parent Institution" 
* Setting time to treat
gen timeToTreat= (year - year_)
tab timeToTreat
* Ihs transformation 
gen ihs_aggregate_students_origin = log(aggregate_students_origin + sqrt(aggregate_students_origin^2+1) )
* log_transformation
gen log_aggregate_students_origin = log(1+aggregate_students_origin)
gen log_aggregate_students_origin_ = log(aggregate_students_origin)
bysort Name_Institution year: egen aggregate_nt_studs = total(aggregate_students_origin)
duplicates drop Name_Institution year, force
* Event DD PPML
replace dumvarIndia_treatlist=0 if dumvarIndia_treatlist==.
* Alternative SE clustering
quietly ppmlhdfe aggregate_nt_studs ib2008.year##i.dumvarIndia_treatlist if sample_==1 & year>=2003 & year<=2017, absorb(Insitution_Code_5Digit year) cluster(Insitution_Code_5Digit)
local plotopts ytitle("Estimate and 95% Conf. Int.") title("India")
coefplot, vertical yline(0) ciopts(recast(rcap)) xlabel(,angle(45)) `plotopts' drop(_cons) scheme(s1mono) xline(5.5)
restore

* INDIA - Treated nationalities * 
preserve
keep if Origin=="India"
gen year_=2009
foreach v of var year_{
	replace `v'=. if dumvarIndia_treatlist==. 
}
move year_ year
move dumvarIndia_treatlist  year_
drop Province_of_Study ijt Province Origin
move treatment_var aggregate_students_origin 
* 2010
replace year_=2010 if Name_Institution=="College of New Caledonia" | Name_Institution=="College of the Rockies" | Name_Institution=="Conestoga College of Applied Arts and Technology" | Name_Institution=="Confederation College of Applied Arts and Technology" | Name_Institution=="Douglas College" | Name_Institution=="Durham College of Applied Arts and Technology" | Name_Institution=="Fanshawe College of Applied Arts and Technology" | Name_Institution=="Kwantlen Polytechnic University" | Name_Institution=="Lambton College of Applied Arts and Technology" | Name_Institution=="Medicine Hat College" | Name_Institution=="North Island College" | Name_Institution=="Northern Lights College" | Name_Institution=="Okanagan College" | Name_Institution=="Selkirk College" | Name_Institution=="St. Clair College of Applied Arts and Technology" | Name_Institution=="St. Lawrence College of Applied Arts and Technology - Parent Institution" 
* 2011
replace year_=2011 if Name_Institution=="New Brunswick Community College" | Name_Institution=="Saskatchewan Polytechnic - Parent Institution"
* 2012
replace year_=2012 if Name_Institution=="Langara College"
* 2015
replace year_=2015 if Name_Institution=="Assiniboine Community College" | Name_Institution=="Canadore College of Applied Arts and Technology" | Name_Institution=="Collège LaSalle" | Name_Institution=="Lakeland College" | Name_Institution=="NorQuest College" | Name_Institution=="Sault College of Applied Arts and Technology - Parent Institution" 
* Setting time to treat
gen timeToTreat= (year - year_)
tab timeToTreat
* Ihs transformation 
gen ihs_aggregate_students_origin = log(aggregate_students_origin + sqrt(aggregate_students_origin^2+1) )
* log_transformation
gen log_aggregate_students_origin = log(1+aggregate_students_origin)
gen log_aggregate_students_origin_ = log(aggregate_students_origin)
* Event DD PPML
replace dumvarIndia_treatlist=0 if dumvarIndia_treatlist==.
* Alternative SE clustering
quietly ppmlhdfe aggregate_students_origin ib2008.year##i.dumvarIndia_treatlist if sample_==1 & year>=2003 & year<=2017, absorb(Insitution_Code_5Digit year) cluster(Insitution_Code_5Digit)
local plotopts ytitle("Estimate and 95% Conf. Int.") title("India")
coefplot, vertical yline(0) ciopts(recast(rcap)) xlabel(,angle(45)) `plotopts' drop(_cons) scheme(s1mono) xline(5.5)
restore



* VIETNAM - Non-treated nationalities * 
preserve
keep if Origin=="Vietnam"
gen year_=2016
foreach v of var year_{
	replace `v'=. if dumvarVietnam_treatlist==. 
}
move year_ year
move dumvarVietnam_treatlist  year_
drop Province_of_Study ijt Province Origin
move treatment_var aggregate_students_origin 
* 2018
replace year_=2018 if Name_Institution=="Collège LaSalle" | Name_Institution=="Dalhousie University" | Name_Institution=="Northern Lights College" | Name_Institution=="Sault College of Applied Arts and Technology - Parent Institution" | Name_Institution=="Sheridan College Institute of Technology" | Name_Institution=="St. Lawrence College of Applied Arts and Technology - Parent Institution" | Name_Institution=="Thompson Rivers University - Parent Institution" | Name_Institution=="Vancouver Island University"
gen timeToTreat= (year - year_)
tab timeToTreat
* Ihs transformation 
gen ihs_aggregate_students_origin = log( aggregate_students_origin + sqrt(aggregate_students_origin^2+1) )
* log_transformation
gen log_aggregate_students_origin = log(1+aggregate_students_origin)
gen log_aggregate_students_origin_ = log(aggregate_students_origin)
* running the event
replace dumvarVietnam_treatlist=0 if dumvarVietnam_treatlist==.
* Alternative SE clustering
quietly ppmlhdfe aggregate_students_origin ib2015.year##i.dumvarVietnam_treatlist if sample_==1 & year>=2010 & year<=2017, absorb(Insitution_Code_5Digit year) cluster(Insitution_Code_5Digit)
estimates store es_3
local plotopts ytitle("Estimate and 95% Conf. Int.") title("Vietnam")
coefplot, vertical yline(0) ciopts(recast(rcap)) xlabel(,angle(45)) `plotopts' drop(_cons) scheme(s1mono) xline(5.5)
restore

* VIETNAM * Non-Treated Nationalities * 
preserve
drop if Origin=="India" | Origin=="China" | Origin=="Vietnam"
gen year_=2016
foreach v of var year_{
	replace `v'=. if dumvarVietnam_treatlist==. 
}
move year_ year
move dumvarVietnam_treatlist  year_
drop Province_of_Study ijt Province Origin
move treatment_var aggregate_students_origin 
* 2018
replace year_=2018 if Name_Institution=="Collège LaSalle" | Name_Institution=="Dalhousie University" | Name_Institution=="Northern Lights College" | Name_Institution=="Sault College of Applied Arts and Technology - Parent Institution" | Name_Institution=="Sheridan College Institute of Technology" | Name_Institution=="St. Lawrence College of Applied Arts and Technology - Parent Institution" | Name_Institution=="Thompson Rivers University - Parent Institution" | Name_Institution=="Vancouver Island University"
gen timeToTreat= (year - year_)
tab timeToTreat
* Ihs transformation 
gen ihs_aggregate_students_origin = log(aggregate_students_origin + sqrt(aggregate_students_origin^2+1))
* log_transformation
gen log_aggregate_students_origin = log(1+aggregate_students_origin)
gen log_aggregate_students_origin_ = log(aggregate_students_origin)
bysort Name_Institution year: egen aggregate_nt_studs = total(aggregate_students_origin)
duplicates drop Name_Institution year, force
* running the event
replace dumvarVietnam_treatlist=0 if dumvarVietnam_treatlist==.
* Alternative SE clustering
quietly ppmlhdfe aggregate_nt_studs ib2015.year##i.dumvarVietnam_treatlist if sample_==1 & year>=2010 & year<=2017, absorb(Insitution_Code_5Digit year) cluster(Insitution_Code_5Digit)
local plotopts ytitle("Estimate and 95% Conf. Int.") title("Vietnam")
coefplot, vertical yline(0) ciopts(recast(rcap)) xlabel(,angle(45)) `plotopts' drop(_cons) scheme(s1mono) xline(5.5)
restore
* lags appear as leads and vice versa




* Figure 4 and 5
use "$data/Finalmaster.dta", clear

****************************************
* Graphs - Parallel Trends
preserve
keep if sample_==1
gen code_ = 0
replace code_=1 if Origin=="China" | Origin=="India" | Origin=="Vietnam"
bysort code_ treated_list year: egen Enrolment_Graph = total(aggregate_students_origin)
* LEFT GRAPH // Within Treated Institutions 
twoway (line Enrolment_Graph year if code_==1 & treated_list==1 & year<=2017 & year>=2003) (line Enrolment_Graph year if code_==0 & treated_list==1 & year<=2017 & year>=2003), tline(2008, lpattern(dash_dot)) tline(2009, lpattern(solid)) tline(2015, lpattern(solid)) legend(lab (1 "Treatment") lab(2 "Control") title("Treatment vs Control Origins")) subtitle("Within Treated Institutions") scheme(economist)
restore

* Graphs - Parallel Trends
preserve 
keep if sample_==1
gen code_ = 0
replace code_=1 if Origin=="China" | Origin=="India" | Origin=="Vietnam"
bysort code_ treated_list year: egen Enrolment_Graph = total(aggregate_students_origin)
* Within Non-Treated Institutions 
twoway (line Enrolment_Graph year if code_==1 & treated_list==0 & year<=2017 & year>=2003) (line Enrolment_Graph year if code_==0 & treated_list==0 & year<=2017 & year>=2003), tline(2008, lpattern(dash_dot)) tline(2009, lpattern(solid)) tline(2015, lpattern(solid)) legend(lab (1 "Treatment") lab(2 "Control") title("Treatment vs Control Origins")) subtitle("Within Non-Treated Institutions") scheme(economist)
restore


* GRAPHS \\ Figure 5

use "$dataFinalmaster.dta", clear

*Create treated list for each nationality
gen treatIndia=1 if treatment_var==1 & Origin=="India"
bysort Name_Institution: egen treated_list_India=max(treatIndia)
gen treatChina=1 if treatment_var==1 & Origin=="China" & year ==2012
bysort Name_Institution: egen treated_list_China=max(treatChina)
tab Name_Institution if treated_list_China==1
gen treatVietnam=1 if treatment_var==1 & Origin=="Vietnam"
bysort Name_Institution: egen treated_list_Vietnam=max(treatVietnam)
**Create baseline control group


*INDIA
preserve
*include SPP institutions not participating for Indian students in control group
replace sample_ = 1 if treated_list_India==1 | controlgroup2==1
keep if sample_==1
gen code_ = 0
replace code_=1 if Origin=="India"
*exclude Chinese and Vietnamese students 
drop if  Origin=="China"
drop if  Origin=="Vietnam"
bysort code_ treated_list_India year: egen Enrolment_Graph = total(aggregate_students_origin)
* LEFT * Within Treated Institutions (WNT)
twoway (line Enrolment_Graph year if code_==1 & treated_list_India==1 & year<=2017 & year>=2003) (line Enrolment_Graph year if code_==0 & treated_list_India==1 & year<=2017 & year>=2003), tline(2008, lpattern(dash_dot)) tline(2009, lpattern(shortdash)) tline(2014, lpattern(shortdash)) legend(lab (1 "Treatment") lab(2 "Control") title("Treatment (India) vs Control Origins")) subtitle("Within Treated Institutions") scheme(economist)
* RIGHT * Within Non-Treated Institutions (WT)
twoway (line Enrolment_Graph year if treated_list_India==. & code_==1 & year<=2017 & year>=2003) (line Enrolment_Graph year if treated_list_India==. & code_==0 & year<=2017 & year>=2003), tline(2008, lpattern(dash_dot)) tline(2009, lpattern(shortdash)) tline(2014, lpattern(shortdash)) legend(lab (1 "Treatment") lab(2 "Control") title("Treatment (India) vs Control Origins")) subtitle("Within Non-Treated Institutions") scheme(economist)
restore
*******

*CHINA
preserve
*include SPP institutions not participating for Indian students in control group
replace sample_ = 1 if treated_list_China==1 | controlgroup2==1
keep if sample_==1
gen code_ = 0
replace code_=1 if Origin=="China"  
* exclude Indian and Vietnamese students 
drop if  Origin=="India" 
drop if  Origin=="Vietnam" 
bysort code_ treated_list_China year: egen Enrolment_Graph = total(aggregate_students_origin)
* LEFT * Within Treated Institutions 
twoway (line Enrolment_Graph year if code_==1 & treated_list_China==1 & year<=2014 & year>=2003) (line Enrolment_Graph year if code_==0 & treated_list_China==1 & year<=2014 & year>=2003), tline(2009, lpattern(shortdash))  legend(lab (1 "Treatment") lab(2 "Control") title("Treatment (China) vs Control Origins")) subtitle("Within Treated Institutions") scheme(economist)
* RIGHT *  Within Non-Treated Institutions 
twoway (line Enrolment_Graph year if treated_list_China==. & code_==1 & year<=2014 & year>=2003) (line Enrolment_Graph year if treated_list_China==. & code_==0 & year<=2014 & year>=2003), tline(2009, lpattern(shortdash))  legend(lab (1 "Treatment") lab(2 "Control") title("Treatment (China) vs Control Origins")) subtitle("Within Non-Treated Institutions") scheme(economist)
restore
*******

*VIETNAM
preserve
*include SPP institutions not participating for Indian students in control group
replace sample_ = 1 if treated_list_Vietnam==1 | controlgroup2==1
keep if sample_==1
gen code_ = 0
replace code_=1 if Origin=="Vietnam"  
*exclude Chinese and Vietnamese students
drop if  Origin=="India" 
drop if  Origin=="China" 
bysort code_ treated_list_Vietnam year: egen Enrolment_Graph = total(aggregate_students_origin)

* LEFT * Within Treated Institutions 
twoway (line Enrolment_Graph year if code_==1 & treated_list_Vietnam==1 & year<=2017 & year>=2003) (line Enrolment_Graph year if code_==0 & treated_list_Vietnam==1 & year<=2017 & year>=2003), tline(2015, lpattern(shortdash)) legend(lab (1 "Treatment") lab(2 "Control") title("Treatment (Vietnam) vs Control Origins")) subtitle("Within Treated Institutions") scheme(economist)
* RIGHT * Within Non-Treated Institutions 
twoway (line Enrolment_Graph year if treated_list_Vietnam==. & code_==1 & year<=2017 & year>=2003) (line Enrolment_Graph year if treated_list_Vietnam==. & code_==0 & year<=2017 & year>=2003), tline(2015, lpattern(shortdash)) legend(lab (1 "Treatment") lab(2 "Control") title("Treatment (Vietnam) vs Control Origins")) subtitle("Within Non-Treated Institutions") scheme(economist)
restore
*******




